package com.dmp.onlinestore.model.dao.jdbc;

// TODO Need to resurrect this PersonDao (commented stuff copied from another project as a starting point).

//import java.sql.Connection;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.sql.Statement;
//import java.util.ArrayList;
import java.util.List;
//import com.dmp.onlinestore.system.Settings;
import com.dmp.onlinestore.model.dao.PersonDao;
import com.dmp.onlinestore.model.dao.base.DaoException;
import com.dmp.onlinestore.model.dao.base.DaoTransaction;
import com.dmp.onlinestore.model.dao.jdbc.base.JdbcDao;
import com.dmp.onlinestore.model.dao.jdbc.base.JdbcDaoTransaction;
import com.dmp.onlinestore.model.dto.PersonBean;
//import com.dmp.onlinestore.system.Settings;


// Some future kind of query (reference for future)...
//
//String sql = 
//"select p.f_name as 'First', p.m_name as 'Middle', p.l_name as 'Last', "  +
//"a.street_num as 'Street Number', a.street_name 'Street Name', "          +
//"a.city as 'City', a.state as 'State', "                                  +
//"t.addr_type as 'Addr Type', assoc.is_primary as 'Primary' "              +
//"from person p, assoc_person_addr assoc, addresses a, address_type t "    + 
//"where assoc.person_id = p.person_id "                                    +
//"and assoc.address_id = a.address_id "                                    +
//"and assoc.addr_type_id = t.addr_type_id order by First, Last;";


public class JdbcPersonDao extends JdbcDao implements PersonDao {
	
	public JdbcPersonDao(DaoTransaction transaction) {
		super((JdbcDaoTransaction) transaction);
	}
	
	public List<PersonBean> getPersons() throws DaoException {
		return null;
		
//		if (Settings.CONSOLE_DEBUG_ON) {
//			System.out.println("JdbcPersonDao::getPersons");
//		}
//		String sql = 
//    		"select p.person_id as 'Id', p.f_name as 'First', p.m_name as 'Middle', " +
//    	    "p.l_name as 'Last' from person p order by Last, First, Middle;";
//		try {
//			List<PersonBean> personList = new ArrayList<PersonBean>();
//			Connection conn = getTransaction().getConnection();
//			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(sql);
//			System.out.println("dao-2");
//            while (rs.next()) {
//        	    PersonBean person = new PersonBean(
//        			rs.getInt("Id"), 
//        			rs.getString("First"),
//        			rs.getString("Middle"),
//        			rs.getString("Last"));
//        	    personList.add(person);
//            }
//            return(personList);
//		}
//		catch (SQLException e) {
//	        System.out.println("SQLException: " + e.getMessage());
//	        System.out.println("SQLState: " + e.getSQLState());
//	        System.out.println("VendorError: " + e.getErrorCode());
//	        getTransaction().fail();
//	        throw new DaoException();	        
//		}
	} // getPersons
	
	public PersonBean getPersonById(int personId) throws DaoException {
		return null;
		
//		if (Settings.CONSOLE_DEBUG_ON) {
//			System.out.println("JdbcPersonDao::getPersonById");
//		}
//		String sql = 
//		"select p.person_id as 'Id', p.f_name as 'First', p.m_name as 'Middle', " +
//	    "p.l_name as 'Last' from person p where p.person_id = " + personId + ";";
//		try {
//			Connection conn = getTransaction().getConnection();
//			Statement stmt = conn.createStatement();
//			ResultSet rs = stmt.executeQuery(sql);
//			rs.next();
//    	    PersonBean person = new PersonBean(
//        			rs.getInt("Id"), 
//        			rs.getString("First"),
//        			rs.getString("Middle"),
//        			rs.getString("Last"));
//    	    return(person);
//		}
//		catch (SQLException e) {
//	        System.out.println("SQLException: " + e.getMessage());
//	        System.out.println("SQLState: " + e.getSQLState());
//	        System.out.println("VendorError: " + e.getErrorCode());
//	        getTransaction().fail();
//	        throw new DaoException(""); // satisfy a compile error
//		}
	} // getPersonById
	
	
	public void addPerson(PersonBean person) throws DaoException {
//		if (Settings.CONSOLE_DEBUG_ON) {
//			System.out.println("JdbcPersonDao::addPerson");
//		}
//		if (person.getId() != -1) {
//			// They're already in the database.
//			throw new DaoException("Attempt to re-add person [" + person.toString() + "] to database");
//		}
//		String sql = 
//   		   "insert into PERSON (f_name, m_name, l_name, gender) values ('" +
//		   person.getFirstName() + "', '" + person.getMiddleName() + "', '" +
//		   person.getLastName() + "', 'M');";
//		System.out.println(sql);	
//		try {
//			Connection conn = getTransaction().getConnection();
//			Statement stmt = conn.createStatement();
//			getTransaction().flagUpdate();
//			int count = stmt.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
//			ResultSet rs = stmt.getGeneratedKeys();
//			if (rs.next()) {
//				person.setId(rs.getInt(1));
//		    }
//			System.out.println("count is \"" + count + "\" and id is \"" + person.getId() + "\"");
//			if (person.getId() == -1) {
//				throw new DaoException("Invalid key returned from insert.");
//			}
//		}
//		catch (SQLException e) {
//	        System.out.println("SQLException: " + e.getMessage());
//	        System.out.println("SQLState: " + e.getSQLState());
//	        System.out.println("VendorError: " + e.getErrorCode());
//	        getTransaction().fail();
//	        throw new DaoException(""); // satisfy a compile error
//		}
	} // addPerson
	
}
